package com.xrz.sxm.aj.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.xrz.sxm.aj.activity.R;
import com.xrz.sxm.aj.listener.CloseAnimationListener;
import com.xrz.sxm.aj.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

public class QiyeView extends LinearLayout {
	/** 计算出ScrollView最大的高度 */
	private int m_Scrollview_Max_hight;
	private static final String CLOSE = "close";
	private static final String OPEN = "open";
	private Animation animation_open, animation_close;
	private List<String> titles = new ArrayList<String>();
	private List<String> contents = new ArrayList<String>();
	private List<ScrollView> scrollviews = new ArrayList<ScrollView>();
	private List<RelativeLayout> RelViews = new ArrayList<RelativeLayout>();
	private List<TextView> TextViews = new ArrayList<TextView>();
	private int Height_image, Height;
	private Context context;

	@SuppressLint("NewApi")
	public QiyeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	public QiyeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public QiyeView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	private Bitmap m_Bitmap;

	public void shifang(){

	}

	private void init() {
		setOrientation(VERTICAL);
		Height = ScreenUtil.getInstant().getDisHight(getContext())
				- ScreenUtil.getInstant().getTopViewHeight(getContext())
				- ScreenUtil.getInstant().getTabHeight(getContext());
		Height_image = Height/3;
		m_Bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.gongsijianjie1);
		LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, Height_image);

		ImageView image = new ImageView(getContext());
		int oldWidth = m_Bitmap.getWidth();
		int oldHight = m_Bitmap.getHeight();
		int newWidth = ScreenUtil.getInstant().getDisWidth(getContext());
		int newHight = (oldHight * newWidth) / oldWidth;
		Log.e("----", newHight+"====");
		parm.width = ScreenUtil.getInstant().getDisWidth(getContext());
		parm.height = newHight;
		image.setLayoutParams(parm);
		image.setImageBitmap(m_Bitmap);



		addView(image);
		animation_open = AnimationUtils.loadAnimation(getContext(),
				R.anim.scale_open);
		animation_close = AnimationUtils.loadAnimation(getContext(),
				R.anim.scales_close);
		titles.add("公司简介");
		contents.add(getResources().getString(R.string.about_qiye));
		for (int i = 0; i < titles.size(); i++) {
			setDataView(titles.get(i), contents.get(i), titles.size(), i);
		}
	}

	public void setDataView(String title, String content, int len, int pos) {
		RelativeLayout rel = new RelativeLayout(getContext());
		rel.setBackgroundResource(R.mipmap.item_bg);
		TextView text = new TextView(getContext());
		text.setPadding(5, 5, 5, 5);
		text.setText(title);
		text.setTextColor(Color.WHITE);
		text.setTextSize(20);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.topMargin = 8;
		lp.bottomMargin = 8;
		lp.leftMargin = 5;
		rel.addView(text, lp);
		TextView text1 = new TextView(getContext());
		text1.setTextColor(Color.WHITE);
		text1.setTextSize(20);
		RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		lp1.rightMargin = 8;
		lp1.addRule(RelativeLayout.CENTER_VERTICAL);
		rel.addView(text1, lp1);
		addView(rel);
		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		rel.measure(w, h);
		ScrollView scr = new ScrollView(getContext());
		m_Scrollview_Max_hight = ScreenUtil.getInstant().getDisHight(
				getContext())
				- ScreenUtil.getInstant().getTopViewHeight(getContext())
				- ScreenUtil.getInstant().getTabHeight(getContext())
				- rel.getMeasuredHeight() * len - Height_image;
		LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, m_Scrollview_Max_hight);
		scr.setLayoutParams(parm);
		LinearLayout m_LinearLayout = new LinearLayout(getContext());
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		m_LinearLayout.setLayoutParams(layoutParams);
		m_LinearLayout.setOrientation(VERTICAL);
//		ImageView image=new ImageView(getContext());
//		image.setScaleType(ScaleType.CENTER_CROP);
//		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
//				LayoutParams.MATCH_PARENT, Height_image);
//		image.setLayoutParams(mParams);
//		image.setImageResource(R.drawable.jianjie2);
//		m_LinearLayout.addView(image);
		TextView tv = new TextView(getContext());
		tv.setTextSize(18);
		tv.setPadding(5, 5, 5, 5);
		SpannableStringBuilder style = new SpannableStringBuilder(content);
		String str1 = "4006699178";
		String str2 = "0371-63213869";
		String str3 = "13538609930";
		int index1 = content.indexOf(str1);
		int index2 = content.indexOf(str2);
		int index3 = content.indexOf(str3);
		style.setSpan(new TestSpanClick(str1), index1,index1+str1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		style.setSpan(new TestSpanClick(str2), index2,index2+str2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		style.setSpan(new TestSpanClick(str3), index3,index3+str3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		tv.setMovementMethod(LinkMovementMethod.getInstance());
//		style.setSpan(new ForegroundColorSpan(Color.BLUE),index1,index1+str1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//实现部分文字颜色改变
//		style.setSpan(new ForegroundColorSpan(Color.BLUE),index2,index2+str2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//实现部分文字颜色改变
//		style.setSpan(new ForegroundColorSpan(Color.BLUE),index3,index3+str3.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//实现部分文字颜色改变


		tv.setText(style);
		m_LinearLayout.addView(tv);
		scr.addView(m_LinearLayout);
		if (pos == 0) {
			text1.setText("-");
			rel.setTag("open");
		} else {
			text1.setText("+");
			rel.setTag("close");
			scr.setVisibility(View.GONE);
		}
		scr.setId(Integer.valueOf("1" + pos));
		text1.setId(Integer.valueOf("2" + pos));
		rel.setId(pos);
		rel.setOnClickListener(new Click(pos));
		scrollviews.add(scr);
		RelViews.add(rel);
		TextViews.add(text1);
		addView(scr);
	}

	private class TestSpanClick extends ClickableSpan {
		private String str;

		public TestSpanClick(String str) {
			this.str = str;
		}

		@Override
		public void onClick(View widget) {
			Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str));
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(intent);
		}
	}

	/**
	 * 控制切换
	 *
	 * @param view
	 *            点击的View
	 * @param ContentView
	 *            显示内容的View
	 * @param stateView
	 *            加减号的TextView
	 */
	private void changeState(View view, View ContentView, TextView stateView) {
		String tag = (String) view.getTag();
		if (tag.equals(CLOSE)) {
			ContentView.setVisibility(View.VISIBLE);
			ContentView.startAnimation(animation_open);
			view.setTag(OPEN);
			stateView.setText("-");
			for (RelativeLayout rel : RelViews) {
				if (rel.getId() != view.getId()) {
					rel.setTag(CLOSE);
				}
			}
			for (ScrollView mScrollView : scrollviews) {
				if (mScrollView.getId() != ContentView.getId()) {
					mScrollView.setVisibility(View.GONE);
				}
			}
			for (TextView mTexView : TextViews) {
				if (mTexView.getId() != stateView.getId()) {
					mTexView.setText("+");
				}
			}

		} else {
			ContentView.startAnimation(animation_close);
			animation_close.setAnimationListener(new CloseAnimationListener(
					ContentView));
			view.setTag(CLOSE);
			stateView.setText("+");
		}
	}

	class Click implements OnClickListener {
		int pos;

		public Click(int pos) {
			this.pos = pos;
		}

		@Override
		public void onClick(View v) {
			changeState(v, scrollviews.get(pos), TextViews.get(pos));
		}
	}
}
